package com.lsu.command.homework;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体的命令类
 *
 * @Author wang suo
 * @Date 2020/10/20 0020 19:12
 * @Version 1.0
 */
public class ConcreteCommand implements Command {

    private MakeDir makeDir;
    private List<String> dirPathList;
    private String preMakeDirPath;

    public ConcreteCommand(MakeDir makeDir) {
        this.makeDir = makeDir;
        this.dirPathList = new ArrayList<>();
        this.preMakeDirPath = null;
    }

    @Override
    public void execute(String path) {
        boolean b = makeDir.createDir(path);
        if (b) {
            dirPathList.add(path);
        }
        preMakeDirPath = path;
    }

    @Override
    public void undo() {
        int size = dirPathList.size();
        if (size > 0) {
            boolean b = makeDir.deleteDir(dirPathList.get(size - 1));
            if (b) {
                dirPathList.remove(size - 1);
            }
        } else {
            System.out.println("没有需要撤销的操作!");
        }
    }

    @Override
    public void redo() {
        if (preMakeDirPath != null && !"".equals(preMakeDirPath)) {
            boolean b = makeDir.createDir(preMakeDirPath);
            if (b) {
                dirPathList.add(preMakeDirPath);
            }
        } else {
            System.out.println("没有可以重复的操作!");
        }
    }
}
